//
//  MyPingLunCont.m
//  ShopDemo
//
//  Created by Sunshine on 2018/8/7.
//  Copyright © 2018年 飞扬的青春. All rights reserved.
//

#import "MyPingLunCont.h"
#import "MyPingLunCell.h"
#import "SecondDetailCon.h"
@interface MyPingLunCont ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *listTableview;
@property(nonatomic,strong)NSMutableArray *contentArray;
@property(nonatomic,assign)NSInteger index;
@property(nonatomic,assign)NSInteger page;

@end

@implementation MyPingLunCont
-(NSMutableArray *)contentArray{
    if (_contentArray) {
        _contentArray = [NSMutableArray array];
    }
    return _contentArray;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.index = 1;
    self.page = 0;
    self.contentArray = [NSMutableArray array];
    NSString *strurl = [NSString stringWithFormat:@"/dashop/news/app/list.do?INNER_CODE=%@&CurrentPage=%ld",[UserDefault objectForKey:LogUserID],self.index];
    NSString *endstr = [MethodClass returnUrlSetStrWSDLURL:strurl];
    [self contentDataMethodWithUrl:endstr];
}
- (void)viewDidLoad {
    [super viewDidLoad];
   
    [self createTableviewMethod];
    self.listTableview.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        if (self.page == 10 ) {
            self.index ++;
            NSString *strurl = [NSString stringWithFormat:@"/dashop/news/app/list.do?INNER_CODE=%@&CurrentPage=%ld",[UserDefault objectForKey:LogUserID],self.index];
            NSString *endstr = [MethodClass returnUrlSetStrWSDLURL:strurl];
            [self contentDataMethodWithUrl:endstr];
        }else{
            [self.listTableview.mj_footer endRefreshingWithNoMoreData];
        }
    }];
}

//文章列表
-(void)contentDataMethodWithUrl:(NSString *)url{
    [YQNetworking getWithUrl:url refreshRequest:YES cache:NO params:nil progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
    } successBlock:^(id response) {
        NSArray *arr = response;
        self.page = arr.count;
        for (NSDictionary *dic in arr) {
            [self.contentArray addObject:dic];
        }
        [[NSOperationQueue mainQueue]addOperationWithBlock:^{
            [self.listTableview reloadData];
        }];
    } failBlock:^(NSError *error) {
        
    }];
    
}

-(void)createTableviewMethod{
    self.listTableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 120*kJLXWidthScale) style:UITableViewStylePlain];
    [self.view addSubview:self.listTableview];
    if (kScreenHeight == 812) {
        self.listTableview.sd_layout
        .bottomSpaceToView(self.view, 34);
    }
    self.listTableview.dataSource = self;
    self.listTableview.delegate = self;
    [self.listTableview registerClass:[MyPingLunCell class] forCellReuseIdentifier:@"onecell"];
    //分隔线
    self.listTableview.separatorStyle = UITableViewCellAccessoryNone;
    //去掉尾部多余的空白
    [self.listTableview setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
    
}

#pragma mark --UITableViewDelegate,UITableViewDataSource--


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.contentArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MyPingLunCell *cell = [tableView dequeueReusableCellWithIdentifier:@"onecell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSDictionary *dic = self.contentArray[indexPath.row];
    cell.namelab.text = [NSString stringWithFormat:@"来源：%@",dic[@"STEMFROM"]];
    cell.titlab.text = [NSString stringWithFormat:@"%@",dic[@"TITLE"]];
    cell.contentlab.text = [NSString stringWithFormat:@"%@",dic[@"SHORTCONTENT"]];
    cell.datelab.text = [NSString stringWithFormat:@"%@",dic[@"CTIME"]];
    NSString *strurl = [NSString stringWithFormat:@"/dashop/%@",dic[@"SFID"]];
    NSString *endstr = [MethodClass returnUrlSetStrWSDLURL:strurl];
    [cell.titimagevi sd_setImageWithURL:[NSURL URLWithString:endstr]];
    if ([dic[@"SEX"]isEqualToString:@"SEX"]) {
        cell.seximagevi.image = [UIImage imageNamed:@"boy.png"];
    }else{
        cell.seximagevi.image = [UIImage imageNamed:@"girl.png"];
    }
    
    return cell;
    
    
    
}







-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 140*kJLXWidthScale;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dic = self.contentArray[indexPath.row];
    SecondDetailCon *dvc = [[SecondDetailCon alloc]init];
    dvc.detailDic = dic;
    dvc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:dvc animated:YES];
}

@end
